package movie.test.order;

import java.text.ParseException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import movie.test.as.order.service.OrderService;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageInfo;

/**
 * 定位是一家电影院 添加购物车
 * 
 * @author leiwenyang
 *
 */
@RestController
@RequestMapping("/orderController")
public class OrderController {

	private static Logger logger = LoggerFactory.getLogger(OrderController.class);

	@Autowired
	private OrderService orderServiceImpl;

	/**
	 * 添加订单
	 * 
	 * @param objs
	 * @param request
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/addOrderInfo")
	public Object addOrderInfo(@RequestBody JSONObject objs, HttpServletRequest request) {

		try {
			logger.debug("添加购物车开始");
			Map<String, Object> addOrderInfo = orderServiceImpl.addOrderInfo(objs);
			logger.info("添加购物车结束");
			Map<String, Object> resultMap = new HashMap<String, Object>();
			resultMap.put("result", addOrderInfo);
			return resultMap;
		} catch (Exception e) {
			e.printStackTrace();
		}

		return "";
	}

	@ResponseBody
	@RequestMapping("/getUserInfo")
	public Object getUserInfo(@RequestBody JSONObject objs, HttpServletRequest request) {

		try {
			logger.debug("查询用户是否登录");
			Map<String, Object> userInfo = orderServiceImpl.getUserInfo(objs);
			logger.info("查询用户登录结束");
			Map<String, Object> resultMap = new HashMap<String, Object>();
			resultMap.put("result", userInfo);
			return resultMap;
		} catch (Exception e) {
			e.printStackTrace();
		}

		return "";
	}

	/**
	 * 订单提交
	 * 
	 * @param objs
	 * @param request
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/getOrderByUserId")
	public Object getOrderByUserId(HttpServletRequest request) {

		try {
			logger.debug("查询订单开始");
			List<Map<String, Object>> orderList = orderServiceImpl.getOrderByUserId();
			logger.info("查询订单结束");
			Map<String, Object> resultMap = new HashMap<String, Object>();
			resultMap.put("result", orderList);
			return resultMap;
		} catch (Exception e) {
			e.printStackTrace();
		}

		return "";
	}

	// 查询订单编号
	@ResponseBody
	@RequestMapping("/getOrderNoById")
	public Object getOrderNoById(@RequestBody JSONObject objs, HttpServletRequest request) {

		try {
			logger.debug("查询订单编号");
			List<Map<String, Object>> orderNo = orderServiceImpl.getOrderNoById(objs);
			logger.info("查询订单编号");
			Map<String, Object> resultMap = new HashMap<String, Object>();
			resultMap.put("result", orderNo);
			return resultMap;
		} catch (Exception e) {
			e.printStackTrace();
		}

		return "";
	}

	/**
	 * 订单提交
	 * 
	 * @param objs
	 * @param request
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/updateOrderById")
	public Object updateOrderById(@RequestBody JSONObject objs, HttpServletRequest request) {

		try {
			logger.debug("更新订单开始");
			int count = orderServiceImpl.updateOrderById(objs);
			logger.info("更新订单结束");
			Map<String, Object> resultMap = new HashMap<String, Object>();
			resultMap.put("result", count);
			return resultMap;
		} catch (Exception e) {
			e.printStackTrace();
		}

		return "";
	}

	/**
	 * 订单提交
	 * 
	 * @param objs
	 * @param request
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/updateSubmitOrderById")
	public Object updateSubmitOrderById(@RequestBody JSONObject objs, HttpServletRequest request) {

		try {
			logger.debug("支付订单");
			int count = orderServiceImpl.updateSubmitOrderById(objs);
			logger.info("支付订单成功");
			Map<String, Object> resultMap = new HashMap<String, Object>();
			resultMap.put("result", count);
			return resultMap;
		} catch (Exception e) {
			e.printStackTrace();
		}

		return "";
	}

	/**
	 * 退票
	 * 
	 * @param objs
	 * @param request
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/updateReturnTicket")
	public Object updateReturnTicket(@RequestBody JSONObject objs, HttpServletRequest request) {

		try {
			logger.debug("退票开始---");
			int count = orderServiceImpl.updateReturnTicket(objs);
			logger.info("退票成功");
			Map<String, Object> resultMap = new HashMap<String, Object>();
			resultMap.put("result", count);
			return resultMap;
		} catch (Exception e) {
			e.printStackTrace();
		}

		return "";
	}

	/*	*//**
			 * 更新座位信息
			 */
	@ResponseBody
	@RequestMapping("/updateSeatsById")
	public Object updateSeatsById(@RequestBody JSONObject objs, HttpServletRequest request) {

		try {
			logger.debug("更新座椅信息");
			int count = orderServiceImpl.updateSeatsById(objs);
			logger.info("更新座椅成功");
			Map<String, Object> resultMap = new HashMap<String, Object>();
			resultMap.put("result", count);
			return resultMap;
		} catch (Exception e) {
			e.printStackTrace();
		}

		return "";
	}

	/**
	 * 查询所有的订单
	 * 
	 * @param objs
	 * @param request
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/getOrderListsByUserId")
	public Object getOrderListsByUserId(HttpServletRequest request) {

		try {
			logger.debug("查询我的订单开始");
			List<Map<String, Object>> orderList = orderServiceImpl.getOrderListsByUserId();
			logger.info("查询我的订单结束");
			Map<String, Object> resultMap = new HashMap<String, Object>();
			resultMap.put("result", orderList);
			return resultMap;
		} catch (Exception e) {
			e.printStackTrace();
		}

		return "";
	}

	@ResponseBody
	@RequestMapping("/getHistoryOrderListById")
	public Object getHistoryOrderListById(@RequestBody JSONObject objs, HttpServletRequest request) {

		try {
			logger.debug("查询我的订单开始");
			List<Map<String, Object>> orderList = orderServiceImpl.getHistoryOrderListById(objs);
			logger.info("查询我的订单结束");
			Map<String, Object> resultMap = new HashMap<String, Object>();
			resultMap.put("result", orderList);
			return resultMap;
		} catch (Exception e) {
			e.printStackTrace();
		}

		return "";
	}

	@ResponseBody
	@RequestMapping("/getOrderListById")
	public Object getOrderListById(@RequestBody JSONObject objs, HttpServletRequest request) {

		try {
			logger.debug("查询我的订单开始");
			List<Map<String, Object>> orderList = orderServiceImpl.getOrderListById(objs);
			logger.info("查询我的订单结束");
			Map<String, Object> resultMap = new HashMap<String, Object>();
			resultMap.put("result", orderList);
			return resultMap;
		} catch (Exception e) {
			e.printStackTrace();
		}

		return "";
	}

	// 后台管理
	@ResponseBody
	@RequestMapping("/getOrderList")
	public Object getOrderList(@RequestBody JSONObject objs, HttpServletRequest request) {

		try {
			logger.debug("查询的订单开始");
			PageInfo<Map<String, Object>> orderList = orderServiceImpl.getOrderList(objs);
			logger.info("查询我的订单结束");
			Map<String, Object> resultMap = new HashMap<String, Object>();
			resultMap.put("result", orderList);
			return resultMap;
		} catch (Exception e) {
			e.printStackTrace();
		}

		return "";
	}

	// 后台管理
	@ResponseBody
	@RequestMapping("/deleteOrders")
	public Object deleteOrders(@RequestBody JSONObject objs, HttpServletRequest request) {

		try {
			logger.debug("删除订单开始");
			int count = orderServiceImpl.deleteOrders(objs);
			logger.info("删除订单结束");
			Map<String, Object> resultMap = new HashMap<String, Object>();
			resultMap.put("result", count);
			return resultMap;
		} catch (Exception e) {
			e.printStackTrace();
		}

		return "";
	}

	// 票房统计
	@ResponseBody
	@RequestMapping("/getTotalTicketList")
	public Object getTotalTicketList(@RequestBody JSONObject objs, HttpServletRequest request) throws ParseException {

		try {
			logger.debug("票房统计开始");
			PageInfo<Map<String, Object>> orderList = orderServiceImpl.getTotalTicketList(objs);
			logger.info("票房统计结束");
			Map<String, Object> resultMap = new HashMap<String, Object>();
			resultMap.put("result", orderList);
			return resultMap;
		} catch (Exception e) {
			e.printStackTrace();
		}

		return "";
	}

	// 查询今日票房纪录
	@ResponseBody
	@RequestMapping("/getTodayTickets")
	public Object getTodayTickets(HttpServletRequest request) {

		try {
			logger.debug("今日票房查询开始");
			List<Map<String, Object>> orderList = orderServiceImpl.getTodayTickets();
			logger.info("今日票房查询结束");
			Map<String, Object> resultMap = new HashMap<String, Object>();
			resultMap.put("result", orderList);
			return resultMap;
		} catch (Exception e) {
			e.printStackTrace();
		}

		return "";
	}
}
